<?php

class Utils {
	
	/**
	 * Get current URL path
	 *
	 * @return String URL
	 */
	 
	public static function getWWW() {
		$pageURL = 'http';
		if ( isSet( $_SERVER["HTTPS"] ) ) {
			$pageURL .= "s";
		}
		$pageURL .= "://";
		if ( $_SERVER["SERVER_PORT"] != "80" ) {
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		} else {
			$pageURL .= $_SERVER["SERVER_NAME"].dirname($_SERVER["REQUEST_URI"]).'/';
		}
		return $pageURL;
	}
	
	/**
	 * Extract first letter from the string
	 *
	 * @return String letter
	 */
	 
	public static function getFirstLetter( $str ) {
		return substr($str, 0, 1);
	}
	
	
	/**
	 * Print alphabet jump list to devices
	 */
	
	public static function printDeviceAlphabet( $devices ) {
		$letters = array();
		foreach( $devices as $device ) {
			$letters[] = self::getFirstLetter( $device->getModel() );
		}
		
		foreach( array_unique( $letters ) as $letter ) {
			echo '<a href="#'.$letter.'">'.$letter.'</a> ';
		}
	}
	
	/**
	 * Get current page ID
	 *
	 * @return String ID
	 */
	 
	public static function getPage() {
		if ( isSet( $_GET['page'] ) ) {
	 		return strip_tags( $_GET['page'] );
	 	}
	 	return false;
	}
	 
	/**
	 * Get active page CSS class
	 *
	 * @return String CSS class
	 */
	
	public static function getActivePageClass( $page ) {
		if ( self::getPage() === $page ) {
			echo 'active_page';
		}
		echo '';
	}

	/**
	 * Get page title
	 */
	
	public static function getPageTitle() {
		$title = '';
		switch( self::getPage() ) {
			case 'activate_app':
				$title = 'Rakenduse aktiveerimine';
				break;
			case 'users':
				$title = 'Rentijad';
				break;
			case 'contracts':
				$title = 'Lepingud';
				break;
			case 'recent':
				$title = 'Hiljutised seadmed';
				break;
			case 'out':
				$title = 'Välja antud seadmed';
				break;
			default:
				$title = 'Kohal olevad seadmed';
		}
		echo $title;
	}
	
	/**
	 * Get extra page links
	 */
	
	public static function getExtraPageLinks() {
		switch( self::getPage() ) {
			case false:
				echo '<a href="#" style="float:right" id="addDeviceLink">Lisa seade</a>';
				break;
		}
	}
	
	/**
	 * Get $_REQUEST device_id value
	 *
	 * @return String value or false
	 */
	
	public static function getRequestDeviceID() {
		if ( isSet( $_REQUEST['device-id'] ) ) {
			return DatabaseHelper::escape( $_REQUEST['device-id'] );
		}
		return false;
	}
	
	/**
	 * unMarshal $_POST to device object
	 *
	 * @return Device object or false
	 */
	
	function unMarshalDevice() {
		if ( isSet( $_POST['device_model'] ) ) {
			$device_data = array( 'mudel' => DatabaseHelper::escape( ucwords( $_POST['device_model'] ) ),
							'kirjeldus' => DatabaseHelper::escape( $_POST['device_description'] ),
							'seerianumber' => DatabaseHelper::escape( $_POST['device_serial'] ),
							'hind' => DatabaseHelper::escape( $_POST['device_value'] ) );
			if ( ( $device_id = self::getRequestDeviceID() ) ) {
				$device_data['seadme_id'] = $device_id;
			}
			return new Device( (object) $device_data );
		}
		return false;
	}
}

?>